/**
 * 
 */
package server;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import server.model.players.Client;

/**
 * @author Austin
 *
 */
public class SQL {
	
	
	public static String getHost() {
		////jdbc:mysql://clan-recon.info:3306/clanrec1_arbiter
		return "jdbc:mysql://"+Config.SQL_HOST+":"+Config.SQL_PORT+"/"+Config.SQL_DatabaseName;
	}
	
	public static ResultSet getResultSet(String username) {
		 try {
			conn = DriverManager.getConnection("jdbc:mysql://clan-recon.info:3306/"+Config.SQL_DatabaseName, 
					 Config.SQL_USERNAME, Config.SQL_PASSWORD);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	try {
		ResultSet i = conn.createStatement().executeQuery("SELECT password FROM `players` WHERE username='"+username+"'");
	} catch (SQLException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}
	try {
		conn.close();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
	}
	
	
	
	public static boolean correctInfo(String username, String password) throws SQLException {
		 conn = DriverManager.getConnection("jdbc:mysql://clan-recon.info:3306/"+Config.SQL_DatabaseName, 
				 Config.SQL_USERNAME, Config.SQL_PASSWORD);
	ResultSet i = conn.createStatement().executeQuery("SELECT password FROM `players` WHERE username='"+username+"'");
	if(i == null) return false;
	if(i.getWarnings() != null)
	if(i.getWarnings().toString().equals("null")) return false;
	i.beforeFirst();
	int passIndex = 0;
	int userIndex = 0;
	i.next();
	passIndex = i.findColumn("password");
String pass = i.getString(passIndex);
conn.close();
	return pass.equals(password);
	}


		static Connection conn;
		
		public SQL(Client client) {
			
		}
		
		public static void main(String[] args) {
			try {
				  Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
				  createUser("hola", "");
				System.out.println(correctInfo("hola", ""));
				
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		


		public static String getFromArray(int[][] array) {
			String temp = "";
			//stacksep = -
			//itemsep = :
			for(int i = 0; i < array.length; i++) {
				temp += array[i][0];
				temp += "-";
				temp += array[i][1];
				temp += ":";
			}
			//Remove last : to not confuse parsing
			temp = temp.substring(0, temp.length() - 1);
			return temp;	
}
		
		public static int[][] getFromString(String s) {
			//Get Each Item and loop, duh
			String[] args = s.split(":");
			int[][] temp = new int[500][500];
			int at = -1;
			for(int i = 0; i < args.length; i++) {
				String[] stack = args[i].split("-");
				temp[at++][0] = Integer.parseInt(stack[0]);
				temp[at][1] = Integer.parseInt(stack[1]);
			}
			return temp;
		}
		
		public static void createUser(String username, String password) throws ClassNotFoundException, SQLException {
			 //Class.forName("org.gjt.mm.mysql.Driver"); //Load the driver
			

					conn = DriverManager.getConnection("jdbc:mysql://clan-recon.info:3306/"+Config.SQL_DatabaseName, Config.SQL_USERNAME, Config.SQL_PASSWORD);

	            
	         
	            		String query = "INSERT INTO `"+Config.SQL_TableName+"`(`inventory`,`bank`,`equipment`,`username`,`password`,`skills`) VALUES ('"+getFromArray(Config.STARTING_INVENTORY)
	            		+"', '"+getFromArray(Config.STARTING_EQUIPMENT)+"', '" + getFromArray(Config.STARTING_EQUIPMENT) + "', '"+username+"', '"+password+"','ha')";
	            		
							conn.createStatement().execute(query);
					
						conn.close();
					}
		

	


}
